﻿using DataFileTreeNoe;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataFileTreeNode
{
  
        //[Serializable]
        //public class ListCustomProperty<T> : BindingList<T> where T : class
        //{
        //    //HashSet只存储NodeText，用来判断是否有重名，HashSet内容顺序不要求
        //    HashSet<string> hashSet = new HashSet<string>();
        //    public new void Add(T item)
        //    {
        //        DataFileTreeNodeProperty cus = item as DataFileTreeNodeProperty;
        //        string key = "";
        //        CheckName(cus.Key, ref key);
        //        cus.Key = key;
        //        item = cus as T;
        //        // 调用基类的Add方法
        //        base.Add(item);
        //    }
        //    private void CheckName(string name, ref string outName)
        //    {
        //        bool canAdd = hashSet.Add(name);
        //        if (!canAdd)
        //        {
        //            if (name.Length > 50)
        //            {
        //                outName = name;
        //                return;
        //            }
        //            name = name + "_copy";
        //            CheckName(name, ref outName);
        //        }
        //        else
        //        {
        //            outName = name;
        //            return;
        //        }
        //    }

        //    public new void Insert(int index, T item)
        //    {
        //        DataFileTreeNodeProperty cus = item as DataFileTreeNodeProperty;
        //        string key = "";
        //        CheckName(cus.Key, ref key);
        //        cus.Key = key;
        //        item = cus as T;
        //        base.Insert(index, item);
        //    }

        //    public new void Remove(T item)
        //    {
        //        DataFileTreeNodeProperty cus = item as DataFileTreeNodeProperty;
        //        hashSet.Remove(cus.Key);
        //        base.Remove(item);
        //    }



        //    //自己新增的调换元素位置的方法
        //    public void Swap(int originalIndex, int newIndex)
        //    {
        //        if (originalIndex == newIndex) return;

        //        DataFileTreeNodeProperty item = this[originalIndex] as DataFileTreeNodeProperty;
        //        hashSet.Remove(item.Key);
        //        this.RemoveAt(originalIndex);
        //        if (newIndex > originalIndex)
        //        {
        //            // 如果新索引在原索引之后，需要减去1来考虑已移除的元素
        //            newIndex--;
        //        }
        //        this.Insert(newIndex, item as T);
        //    }

        //}



        [Serializable]
        public class CustomList<T> : List<T> where T : class
        {
            //hashSet存储所有子节点的NodeText
            HashSet<string> hashSet = new HashSet<string>();
            //Add方法不支持重写（Override），使用new关键字覆盖基类的Add方法
            public new void Add(T item)
            {
            if (typeof(T)==typeof(DataFileTreeNode))
            {
                DataFileTreeNode cus = item as DataFileTreeNode;
                string nodeText = "";
                CheckName(cus.NodeText, ref nodeText);
                cus.NodeText = nodeText;

                string[] fullPath = cus.FullPath.Split('\\');
                //判断Node Text名称是否更改（重名时会更改NodeText），若更改则修改FullPath
                if (cus.FullPath.Split('\\').Last() != nodeText)
                {
                    fullPath[fullPath.Length - 1] = nodeText;
                    cus.FullPath = string.Join("\\", fullPath);
                }

                item = cus as T;

                // 调用基类的Add方法
                base.Add(item);
            }
             
            
            }

            //自己新增的调换元素位置的方法
            public void Swap(int originalIndex, int newIndex)
            {
                if (originalIndex == newIndex) return;

                T item = this[originalIndex];
                this.RemoveAt(originalIndex);

                if (newIndex > originalIndex)
                {
                    // 如果新索引在原索引之后，需要减去1来考虑已移除的元素
                    newIndex--;
                }

                this.Insert(newIndex, item);
            }



            private void CheckName(string name, ref string outName)
            {
                bool canAdd = hashSet.Add(name);
                if (!canAdd)
                {
                    if (name.Length > 50)
                    {
                        outName = name;
                        return;
                    }
                    name = name + "_copy";
                    CheckName(name, ref outName);

                }
                else
                {
                    outName = name;
                    return;
                }
            }

            //移除指定元素
            public new void Remove(T item)
            {
                DataFileTreeNode cus = item as DataFileTreeNode;
                hashSet.Remove(cus.NodeText);
                base.Remove(item);
            }
            public bool Rename(string oldName, string newName)
            {
                if (hashSet.Contains(newName))
                {
                    return false;
                }
                foreach (T Customer in this)
                {
                    DataFileTreeNode treeNodeCustomer = Customer as DataFileTreeNode;
                    if (treeNodeCustomer.NodeText == oldName)
                    {
                        treeNodeCustomer.NodeText = newName;
                        //遍历hashset，更改对应名称
                        for (int i = 0; i < hashSet.Count; i++)
                        {
                            if (hashSet.ElementAt(i) == oldName)
                            {
                                hashSet.Remove(oldName);
                                hashSet.Add(newName);
                            }
                        }
                        return true;
                    }
                }
                return false;
            }
        }
    }

